package com.zx.dsptuneup_TriMic.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zx.dsptuneup_TriMic.App;
import com.zx.dsptuneup_TriMic.Base.BaseFragment;
import com.zx.dsptuneup_TriMic.Common.circleView.CircleControlView;
import com.zx.dsptuneup_TriMic.Data_Bean.ChanelBean;
import com.zx.dsptuneup_TriMic.Enum.MessageReceiveType;
import com.zx.dsptuneup_TriMic.ProtocolTools.ProtocolConversion;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.Tools.EventMessage;
import com.zx.dsptuneup_TriMic.Util.ClickUtil;
import com.zx.dsptuneup_TriMic.Util.Constants;
import com.zx.dsptuneup_TriMic.Util.UserUtil;
import com.zx.dsptuneup_TriMic.bean.BasicResponse;
import com.zx.dsptuneup_TriMic.network.DefaultObserver;
import com.zx.dsptuneup_TriMic.network.NetWorkManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class OutputGainFragment extends BaseFragment {
    @BindView(R.id.db_control)
    CircleControlView circleControlView;

    @BindView(R.id.dbValue)
    TextView dbvalue;

    private View rootView;
    private String TAG = "OutputGainFragment";
    private int[] passagewayGainArr = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};       // volume调节
    private int currentSelTitleIndex = Constants.channelEnum.line1;
    private int[] developChannelArr = {Constants.channelEnum.line1,Constants.channelEnum.line2,
            Constants.channelEnum.line3,Constants.channelEnum.line4,Constants.channelEnum.line5,
            Constants.channelEnum.line6,Constants.channelEnum.line7,Constants.channelEnum.line8}; // 对应开发者通道数据
    private int[] userChannelArr = {Constants.channelEnum.line3};
    public OutputGainFragment() {
        setFragmentTitle("11");
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshUI();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 设置1格代表1db
        circleControlView.setAngleRate(1);
        circleControlView.setDB(-36, 10, 0);
        //设置旋钮是否可旋转
        circleControlView.setCanRotate(true);
        circleControlView.setOnDbChangeListener(new CircleControlView.OnDbChangeListener() {
            @Override
            public void change(int db) {
//                Toast.makeText(App.getContext(), db + "°", Toast.LENGTH_SHORT).show();
                dbvalue.setText(Integer.toString(db));
                passagewayGainArr[currentSelTitleIndex] = db;

                /**
                 * currentSelTitleIndex 做了处理,普通用户模式变成了功放输出为2
                 * */
                int channel = developChannelArr[currentSelTitleIndex];
                App.getContext().getEqData().getOutputChannelArr().get(channel).setVolume(db);

                HashMap map = new HashMap();
                map.put("groups",UserUtil.currentSingleGroup);
                map.put("type",Constants.element_typeEnum.OUTPUT);
                map.put("channel",channel);
                map.put("volume",db);
                //查询指定音频处理器数据

                /**
                 *
                 * */
                NetWorkManager.getInstance().getNetWorkApiService().setVolume(map).compose(OutputGainFragment.this.<BasicResponse<String>>bindToLifecycle())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new DefaultObserver<BasicResponse<String>>() {
                            @Override
                            protected void onSuccess(BasicResponse<String> response) {
                                if (response != null) {
//                                    devicesDetailInfoBeans = response.getData();
//                                    Constants.currentSingleGroup = devicesDetailInfoBeans.getGroup();
//                                    StringUtil.dealWithDevicesInfo(devicesDetailInfoBeans);
                                    Log.e(TAG,"data==="+response.getData());
                                }
                            }
                            @Override
                            protected void onFail(BasicResponse<String> response) {
                                super.onFail(response);

                            }
                            @Override
                            public void onError(@NonNull Throwable e) {
                                super.onError(e);
                            }
                        });

//                App.getContext().getProtocolConversion().setVolume(ProtocolConversion.getFrameId(), Constants.operation_typeEnum.set_device,Constants.element_typeEnum.OUTPUT, channel,db);
            }
        });
    }

    /**
     *  收到消息
     * */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onGetStickyEvent(EventMessage message) {
//        Log.d(TAG,"onReceiveMsg====" + message.toString());
        if (message.getType().equals(MessageReceiveType.OutputTitleIndexChange)){
            this.currentSelTitleIndex = Integer.parseInt(message.getMessage());
            Log.e(TAG," this.currentSelTitleIndex==="+ this.currentSelTitleIndex);

            refreshUI();
        }else  if ( message.getType().equals(MessageReceiveType.MessageRecOutput) ){
            refreshUI();
        }

    }

    /**
     *  刷新UI
     * */
    private void refreshUI() {
        ChanelBean chanelBean = App.getContext().getEqData().getOutputChannelArr().get(currentSelTitleIndex);
        String jsonStr = new Gson().toJson(chanelBean);
//        Log.e("111",jsonStr);
        int curDb = (int) chanelBean.getVolume();
        if(circleControlView != null && dbvalue != null){
            circleControlView.setDB(curDb);
            dbvalue.setText(Integer.toString(curDb));
        }
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initVariables(Bundle bundle) {

    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_input_gain, container, false);
            EventBus.getDefault().register(this);
//            initView(rootView);// 控件初始化
        }
        ButterKnife.bind(this, rootView);

        return rootView;
    }


    @Override
    public void onClick(View v) {
        if (ClickUtil.isFastClick(1000, v)) {
            return;
        }
    }
}